package com.homework.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class connDB {
	public Connection conn = null;
	public Statement stmt = null;
	public ResultSet rs = null;
	public static String propFileName;
	private static Properties prop = new Properties();
	private static String dbClassName;
	private static String dbUrl;
	private static String dbUser;
	private static String dbPwd;
	private static String accessKey;
	private static String secretKey;
	private static String appName;

	/**
	 * 从文件加载数据库连接信息
	 */
	public static void loadProperty() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
//			propFileName = request.getSession().getServletContext().getRealPath("")+ "/dbfile/jdbc.properties";	
			prop.load(request.getSession().getServletContext().getResourceAsStream("/dbfile/jdbc.properties"));
			// ͨ通过文件输入流对象加载Properties文件
			dbClassName = prop.getProperty("jdbc.classname");	
			dbUrl = prop.getProperty("jdbc.url");
			dbUser = prop.getProperty("jdbc.username");
			dbPwd = prop.getProperty("jdbc.password");
			accessKey = prop.getProperty("jdbc.accessKey");
			secretKey = prop.getProperty("jdbc.secretKey");
			appName = prop.getProperty("jdbc.appName");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		loadProperty();
		Connection conn = null;
		try {
			Class.forName(dbClassName).newInstance();
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (Exception ee) {
			ee.printStackTrace();
		}
		if (conn == null) {
			System.err
					.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
							+ dbClassName
							+ "\r\n链接位置:"
							+ dbUrl
							+ "\r\n用户/密码"
							+ dbUser + "/" + dbPwd);
		}
		return conn;
	}

	/**
	 * 功能：执行查询语句
	 * 
	 * @param sql
	 * @return
	 */
	public ResultSet executeQuery(String sql) {
		try {
			conn = getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		}
		return rs;
	}

	/**
	 * 功能:执行更新操作
	 * 
	 * @param sql
	 * @return
	 */
	public int executeUpdate(String sql) {
		int result = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			result = stmt.executeUpdate(sql);
		} catch (SQLException ex) {
			result = 0;
		}
		try {
			stmt.close();
		} catch (SQLException ex1) {
		}
		return result;
	}

	public int executeUpdate_id(String sql) {
		int result = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			result = stmt.executeUpdate(sql);
			String ID = "select @@IDENTITY as id";
			rs = stmt.executeQuery(ID);
			if (rs.next()) {
				int autoID = rs.getInt("id");
				result = autoID;
			}
		} catch (SQLException ex) {
			result = 0;
		}
		return result;
	}

	/**
	 * 功能:关闭数据库的连接
	 * 
	 */
	public void close() {
		// ResultSet rs
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}

		// Statement stmt
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}

		// Connection conn
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}

	public static String getAccessKey() {
		return accessKey;
	}

	public static void setAccessKey(String accessKey) {
		connDB.accessKey = accessKey;
	}

	public static String getSecretKey() {
		return secretKey;
	}

	public static void setSecretKey(String secretKey) {
		connDB.secretKey = secretKey;
	}

	public static String getAppName() {
		return appName;
	}

	public static void setAppName(String appName) {
		connDB.appName = appName;
	}

}
